package com.guguskins.back.controller;


import com.guguskins.back.entity.GuPermission;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.service.GuPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-07-16
 */
@RestController
@RequestMapping("/gu-permission")
public class GuPermissionController {
    @Autowired
    private GuPermissionService permissionService;

    @GetMapping("/back/tree")
    public ResultVo getTree() {
//        permissionService.selectPermissionTreeByUserId();
        List<GuPermission> permissions = permissionService.queryAllMenu();
        return ResultVo.success(permissions);
    }

    @PostMapping("/back/add")
    public ResultVo add(@RequestBody GuPermission permission) {
        int result = permissionService.add(permission);
        if (result > 0) {
            return ResultVo.success("添加成功");
        }
        return ResultVo.fail("添加失败");
    }

    @PutMapping("/back/edit")
    public ResultVo edit(@RequestBody GuPermission permission) {
        int result = permissionService.edit(permission);
        if (result > 0) {
            return ResultVo.success("修改成功");
        }
        return ResultVo.fail("修改失败");
    }

    @GetMapping("/back-detail/{id:\\d+}")
    public ResultVo detail(@PathVariable("id") Integer id) {
        GuPermission permissionEntity = permissionService.getById(id);
        if (permissionEntity != null) {
            return ResultVo.success(permissionEntity);
        }
        return ResultVo.fail("fail", "未知权限");
    }

    @PutMapping("/back/enable")
    public ResultVo enable(@RequestParam(value = "ids", required = true) Integer[] ids) {
        int result = permissionService.updateStatus(ids, 1);
        if (result > 0) {
            return ResultVo.success("启用成功");

        }
        return ResultVo.fail("fail", "启用失败");
    }

    @PutMapping("/back/disable")
    public ResultVo disable(@RequestParam(value = "ids", required = true) Integer[] ids) {
        int result = permissionService.updateStatus(ids, 0);
        if (result > 0) {
            return ResultVo.success("禁用成功");
        }
        return ResultVo.fail("fail", "禁用失败");
    }

}
